package com.nexgo.payment.trans.mainfaceupload;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.UploadType;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.ProgressDialogFragment;
import com.nexgo.payment.widget.TitleBuilder;

public class UploadActivity extends AppCompatActivity implements UploadContract.View {
    public final static String UPLOAD_TYPE = "upload_type";
    private UploadContract.Presenter mPresenter;
    private ProgressDialogFragment mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        new TitleBuilder(this).gone();
        int uploadType = getIntent().getIntExtra(UPLOAD_TYPE, 0);
        new UploadPresenter(this, Repository.getInstance(), UploadType.values()[uploadType]);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    public static Intent newInstance(Context mContext, UploadType uploadType) {
        Intent intent = new Intent(mContext, UploadActivity.class);
        intent.putExtra(UPLOAD_TYPE, uploadType.ordinal());
        return intent;
    }

    @Override
    public void setPresenter(UploadContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void setOnlineProcIndicator(boolean active) {
        if (active) {
            if (mProgressDialog == null) {
                mProgressDialog = new ProgressDialogFragment();
                mProgressDialog.setCancelable(false);
                mProgressDialog.show(getSupportFragmentManager(), null);
            }
        } else {
            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
                mProgressDialog = null;
            }
        }
    }

    @Override
    public void showTip(String tip) {
        if (mProgressDialog == null) {
            setOnlineProcIndicator(true);
        }
        mProgressDialog.setTip(tip);
    }

    @Override
    public void quitUpload(boolean isSuccessful) {
        setResult(isSuccessful ? 0 : -1);
        finish();
    }
}
